package hung.vnptthanhhoa.cookingbook;
import hung.vnptthanhhoa.adapter.TabsPageAdapter;
import hung.vnptthanhhoa.util.SyncFood;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.FragmentTransaction;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.SearchView;

public class MainActivity extends FragmentActivity implements
		ActionBar.TabListener {

	private ViewPager viewPager;
	private TabsPageAdapter mAdapter;
	private ActionBar actionBar;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (!isMyServiceRunning()) {
			Intent serviceIntent = new Intent(getApplicationContext(),
					SyncFood.class);
			startService(serviceIntent);
		}

		setContentView(R.layout.activity_main);

		viewPager = (ViewPager) findViewById(R.id.pager);
		actionBar = getActionBar();
		mAdapter = new TabsPageAdapter(getSupportFragmentManager());
		viewPager.setAdapter(mAdapter);
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		Tab tab_nau_an = actionBar.newTab();
		tab_nau_an.setText(R.string.title_nau_an).setTabListener(this);
		actionBar.addTab(tab_nau_an);

		Tab tab_meo_vat = actionBar.newTab();
		tab_meo_vat.setText(R.string.title_meo_vat).setTabListener(this);
		actionBar.addTab(tab_meo_vat);

		Tab tab_suc_khoe = actionBar.newTab();
		tab_suc_khoe.setText(R.string.title_suc_khoe).setTabListener(this);
		actionBar.addTab(tab_suc_khoe);

		Tab tab_dien_dan = actionBar.newTab();
		tab_dien_dan.setText(R.string.title_dien_dan).setTabListener(this);
		actionBar.addTab(tab_dien_dan);
		viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				// on changing the page
				// make respected tab selected
				actionBar.setSelectedNavigationItem(position);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});

	}

	@Override
	public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		viewPager.setCurrentItem(tab.getPosition());

	}

	@Override
	public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
		// TODO Auto-generated method stub

	}

	private boolean isMyServiceRunning() {
		ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
		for (RunningServiceInfo service : manager
				.getRunningServices(Integer.MAX_VALUE)) {
			if ("hung.vnptthanhhoa.util.SyncFood".equals(service.service
					.getClassName())) {
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
 
        // Associate searchable configuration with the SearchView
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setSearchableInfo(searchManager
                .getSearchableInfo(getComponentName()));
 
        return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return false;
		// switch (item.getItemId()) {
		// case R.id.action_search:
		// Log.e("fd","fds");
		// return true;
		// case R.id.action_favorite:
		// Toast.makeText(getApplicationContext(), "Favorite",
		// Toast.LENGTH_SHORT).show();
		// return true;
		// default:
		// return super.onOptionsItemSelected(item);
		// }

	}

}
